﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL.Helper.Pagination;
using BLL.Helper.Exception;

namespace Web.Helper
{
    public class BaseController : Controller
    {
        protected virtual void HandleBusinessExceptionToView(BusinessException be)
        {
            IDictionary<string, string> errors = new Dictionary<string, string>();
            if (be.FieldWithError != null)
            {
                errors.Add(new KeyValuePair<string, string>(be.FieldWithError, be.Message));
            }
            else
            {
                errors.Add(new KeyValuePair<string, string>(string.Empty, be.Message));
            }
            this.AddModelErrors(errors);
        }

        public void AddModelErrors(IDictionary<string, string> errors)
        {
            if (errors != null)
            {
                foreach (KeyValuePair<string, string> pair in errors)
                {
                    if (base.ModelState.IsValidField(pair.Key))
                    {
                        base.ModelState.AddModelError(pair.Key, pair.Value);
                    }
                }
            }
        }

        protected void MakePager(PageResultDTO pageResult)
        {
            base.ViewData["PageResult"] = pageResult;
        }
    }
}